#include "Key.h"

//六个按键初始化
void Key_Init(void)
{
	rcu_periph_clock_enable(RCU_GPIOE);
	gpio_mode_set(KEY1_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, KEY1_PIN | KEY2_PIN | KEY3_PIN | KEY4_PIN | KEY5_PIN | KEY6_PIN);
}

//按键状态读取
uint8_t Key_State(uint32_t Port,uint16_t Pin)
{
	if(RESET == gpio_input_bit_get(Port, Pin))//检测按键是否被按下
	{
		delay_1ms(100);
		if(RESET == gpio_input_bit_get(Port, Pin))//再次检测按键是否按下
		{
			while(RESET == gpio_input_bit_get(Port, Pin));//等待按键松开
			return 1;
		}
	}
	return 0;
}

//中断初始化，用的是2引脚触发中断
void EXTI_PIN_Init(void)
{
	rcu_periph_clock_enable(RCU_SYSCFG);
	syscfg_exti_line_config(EXTI_SOURCE_GPIOE, EXTI_SOURCE_PIN2);
	exti_init(EXTI_2, EXTI_INTERRUPT, EXTI_TRIG_FALLING);//下降沿触发（按键的波形）
	
	nvic_irq_enable(EXTI2_IRQn, 2, 2);//NVIC分级
	
	exti_interrupt_flag_clear(EXTI_2);//清楚标志位
}

//中断处理函数
void EXTI2_IRQHandler(void)
{
	if(RESET != exti_interrupt_flag_get(EXTI_2))
	{
		gpio_bit_toggle(GPIOA, GPIO_PIN_5);
		exti_interrupt_flag_clear(EXTI_2);
	}
}

//按键处理函数
void Key_Pro(void)
{
	if(Key_State(KEY1_PORT, KEY1_PIN) == 1) rtc_setup();
//	if(Key_State(KEY1_PORT, KEY1_PIN) == 1) LED1_ON();
//	else if(Key_State(KEY2_PORT, KEY2_PIN) == 1) LED1_OFF();
}
